package com.xavier.dong.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author XavierDong
 **/
public class NIOFileChannel02 {

    public static void main(String[] args) throws IOException {


        File file = new File("file01.txt");

        // 文件输入流
        FileInputStream fileInputStream = new FileInputStream(file);

        // 获取 FileChannel
        FileChannel fileChannel = fileInputStream.getChannel();


        // 创建 缓冲区 .依据文件的长度创建
        ByteBuffer byteBuffer = ByteBuffer.allocate(((int) file.length()));

        // 将通道的数据 读到 ByteBuffer 中
        fileChannel.read(byteBuffer);

        // 将 ByteBuffer字节 转成 字符串
        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();

    }
}
